package com.getpebble.android.framework.datalogging;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.pebblekit.PebbleKit;
import java.util.ArrayList;
import java.util.List;

class DataloggingDb extends SQLiteOpenHelper {
    private static final String TAG = DataloggingDb.class.getSimpleName();
    private final SQLiteDatabase mDatabase;
    private final PebbleKit mPebbleKit;

    public DataloggingDb(Context context, PebbleKit pebbleKit) {
        super(context, "data_logging", null, 2);
        if (pebbleKit == null) {
            throw new IllegalArgumentException("pebbleKit cannot be null");
        }
        this.mDatabase = getWritableDatabase();
        this.mPebbleKit = pebbleKit;
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS session (`session_id` INTEGER,  `uuid` VARCHAR,  `timestamp` TIMESTAMP,  `app_log_tag` INTEGER,  `data_type` INTEGER,  `item_size` INTEGER,  `local_session_uuid` VARCHAR PRIMARY KEY,  `is_finished` INTEGER,  `next_data_id_sequence` INTEGER  ); ");
        db.execSQL("CREATE TABLE IF NOT EXISTS item ( `local_session_uuid` VARCHAR,  `data_id` INTEGER,  `data_object` BLOB,   PRIMARY KEY(local_session_uuid, data_id) ); ");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS session");
        db.execSQL("DROP TABLE IF EXISTS item");
        onCreate(db);
    }

    List<DataloggingSession> loadSessionsFromDatabase() {
        ArrayList<DataloggingSession> sessions = new ArrayList();
        Cursor cursor = this.mDatabase.query("session", null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                DataloggingSession session = new DataloggingSession(cursor, this, this.mPebbleKit);
                Trace.debug(TAG, "loading session from database: " + session + " numItems = " + loadItemsForSession(session).size());
                sessions.add(session);
            }
            cursor.close();
        }
        return sessions;
    }

    List<DataloggingItem> loadItemsForSession(DataloggingSession session) {
        ArrayList<DataloggingItem> sessions = new ArrayList();
        String orderBy = "data_id ASC";
        Cursor cursor = this.mDatabase.query("item", null, "local_session_uuid = ?", new String[]{session.getLocalSessionUuid().toString()}, null, null, "data_id ASC");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                DataloggingItem item = new DataloggingItem(cursor, session, this, this.mPebbleKit);
                if (item != null) {
                    sessions.add(item);
                }
            }
            cursor.close();
        }
        return sessions;
    }

    DataloggingItem loadItem(DataloggingSession session, int dataId) {
        DataloggingItem item = null;
        Cursor cursor = this.mDatabase.query("item", null, "local_session_uuid = ? AND data_id = ?", new String[]{session.getLocalSessionUuid().toString(), String.valueOf(dataId)}, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                item = new DataloggingItem(cursor, session, this, this.mPebbleKit);
            }
            cursor.close();
        }
        return item;
    }

    void insertSessionRecord(DataloggingSession session) throws SQLException {
        this.mDatabase.insertOrThrow("session", null, session.getContentValues());
    }

    void updateSessionRecord(DataloggingSession session) {
        this.mDatabase.update("session", session.getContentValues(), "local_session_uuid = ?", new String[]{session.getLocalSessionUuid().toString()});
    }

    void insertItemRecord(DataloggingItem item) throws SQLException {
        this.mDatabase.insertOrThrow("item", null, item.getContentValues());
    }

    void deleteItemRecord(DataloggingItem item) {
        this.mDatabase.delete("item", "local_session_uuid = ? AND data_id = ?", new String[]{item.getSession().getLocalSessionUuid().toString(), String.valueOf(item.getDataId())});
    }

    void deleteSessionRecord(DataloggingSession session) {
        this.mDatabase.delete("session", "local_session_uuid = ?", new String[]{session.getLocalSessionUuid().toString()});
    }

    void beginTransaction() {
        this.mDatabase.beginTransaction();
    }

    void setTransactionSuccessful() {
        this.mDatabase.setTransactionSuccessful();
    }

    void endTransaction() {
        this.mDatabase.endTransaction();
    }
}
